﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WC3Game
{
    public static class Action
    {
        public delegate void HeroDeathEventHandler(String victimName, String killerName, String gameTime, Kill.KillType killType);
        public static HeroDeathEventHandler HeroKilled;

        public static void Parse(String Key, String Data, String Value, SharpPcap.Packets.Util.Timeval timeCode)
        {
            if (Session.CurrentGame == null)
                return;

            if (Data != "Data")
            {
                int dataInt = int.MinValue;
                int.TryParse(Data, out dataInt);

                if (dataInt != int.MinValue)
                {
                    int playerId = dataInt;
                    Player p = Session.CurrentGame.PlayerFromColor(playerId);

                    int valueInt = int.MinValue;
                    int.TryParse(Value, out valueInt);

                    if (Key == "1")
                    {
                        // Kills
                        if (p.KillCount != valueInt)
                        {
                            int i = 0;
                        }
                    }

                    if (Key == "2")
                    {
                        // Deaths
                        if (p.DeathCount != valueInt)
                        {
                            int j = 0;
                        }
                    }

                    if (Key == "3")
                    {
                        // Creep Kills
                        p.CreepKills = valueInt;
                    }

                    if (Key == "4")
                    {
                        // Creep Denies
                        p.CreepDenies = valueInt;
                    }

                    if (Key == "5")
                    {
                        // Assists
                        p.AssistCount = valueInt;
                    }

                    if (Key == "6")
                    {
                        // Current Gold
                        p.CurrentGold = valueInt;
                    }

                    if (Key == "7")
                    {
                        // Neutral Kills
                        p.NeutralKills = valueInt;
                    }

                    Regex regex = new Regex(@"\d_\d");
                    if (regex.IsMatch(Key))
                    {
                        String item = Value;
                        p.Items.Add(item);
                    }

                    if (Key == "9")
                    {
                        if (p.HeroID == String.Empty || p.HeroName != Heroes.GetHeroName(Value))
                        {
                            p.HeroID = Value;
                            Game.SendGameStateChangeMessage(
                                String.Format("{0} recvd hero {1} at {2}",
                                     p.Name,
                                     p.HeroName,
                                     timeCode.ToTimeFormat()
                                     ));
                        }
                    }

                    if (Key == "id")
                    {
                        // Do something. What, I'm not sure.
                        int x = 0;
                    }                    
                }
            }

           if (Key.StartsWith("GameStart"))
            {
                // Game Start. Adjust the game start time.
                Session.CurrentGame.StartTime = new SharpPcap.Packets.Util.Timeval(Session.CurrentGame.StartTime.Seconds + timeCode.Seconds, 0);

                Game.SendGameStateChangeMessage(
                    String.Format("Game {1} started... Adjusting the time by {0}s",
                    timeCode.Seconds,
                    Session.CurrentGame.Name
                    )
                    );
            }

            if (Key == "Winner")
            {
                switch (Int32.Parse(Value))
                {
                    case 1:
                        Session.CurrentGame.Winner = "Sentinal";
                        break;
                    case 2:
                        Session.CurrentGame.Winner = "Scourge";
                        break;
                    default:
                        Session.CurrentGame.Winner = "Unknown";
                        break;
                }
                return;
            }

            if (Key.StartsWith("Courier"))
            {
                int victimColor = Int32.Parse(Key.Substring(7));
                int killerColor = Int32.Parse(Value);

                String killerName = String.Empty;

                Player victim = Session.CurrentGame.PlayerFromColor(victimColor);
                Player killer = Session.CurrentGame.PlayerFromColor(killerColor);

                if (killer == null)
                {
                    switch (killerColor)
                    {
                        case 0:
                            killerName = "Sentinal";
                            break;
                        case 6:
                            killerName = "Scourge";
                            break;
                        default:
                            break;
                    }

                    victim.KilledBy(killerName, timeCode.ToTimeFormat(), Kill.KillType.Courier);
                }
                else
                {
                    victim.KilledBy(killer, timeCode.ToTimeFormat(), Kill.KillType.Courier);
                    killerName = killer.Name;
                }

                if (HeroKilled != null)
                {
                    HeroKilled(victim.Name, killerName, timeCode.ToTimeFormat(), Kill.KillType.Courier);
                }
            }

            if (Key.StartsWith("Tower"))
            {
            }

            if (Key.StartsWith("Rax"))
            {
            }


            if ((Key.StartsWith("Throne") || Key.StartsWith("Tree")))
            {
                // Game over

                if (Int32.Parse(Value) == 0)
                {
                    Game.SendGameStateChangeMessage(
                        String.Format("Game ended with the destruction of {0} at {1}",
                        Key,
                        timeCode.ToTimeFormat())
                        );

                }
                return;
            }

            if (Data == "Global")
            {
                if (Key == "s")
                {
                    // Game over. Start a new one.
                    Session.CurrentGame.Sec = Value;
                    Session.CurrentGame.Complete = true;
                    Game.SendGameStateChangeMessage(String.Format("Game Ended @ {0}, Winner: {1}", Session.CurrentGame.Time, Session.CurrentGame.Winner));
                    Session.AddCurrentGame();
                }

                if (Key == "m")
                {
                    Session.CurrentGame.Min = Value;
                }
            }

            if (Key.StartsWith("Hero"))
            {
                int victimColor = Int32.Parse(Key.Substring(4));
                int killerColor = Int32.Parse(Value);

                String killerName = String.Empty;

                Player victim = Session.CurrentGame.PlayerFromColor(victimColor);
                Player killer = Session.CurrentGame.PlayerFromColor(killerColor);

                if (killer == null)
                {
                    switch (killerColor)
                    {
                        case 0:
                            killerName = "Sentinal";
                            break;
                        case 6:
                            killerName = "Scourge";
                            break;
                        default:
                            killerName = "Unknown Assailant";
                            break;
                    }

                    victim.KilledBy(killerName, timeCode.ToTimeFormat(), Kill.KillType.Hero);
                }
                else
                {
                    victim.KilledBy(killer, timeCode.ToTimeFormat(), Kill.KillType.Hero);
                    killerName = killer.Name;
                }

                if (HeroKilled != null)
                {
                    HeroKilled(victim.Name, killerName, timeCode.ToTimeFormat(), Kill.KillType.Hero);
                }
                return;
            }
        }
    }
}
